package tongwentongshu.com.app.activity;

import android.os.CountDownTimer;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import tongwentongshu.com.app.R;
import tongwentongshu.com.app.bean.OrderEvent;
import tongwentongshu.com.app.contract.BackPasswordContract;
import tongwentongshu.com.app.presenter.BackPasswordPresenter;
import tongwentongshu.com.app.utils.ToastUtil;

/**
 * Created by XiongBin on 2017/7/13.
 */

public class BackPasswordActivity extends BaseActivity implements BackPasswordContract.View {
    @BindView(R.id.et_login_phone)
    EditText etPhone;
    @BindView(R.id.et_login_code)
    EditText etCode;
    @BindView(R.id.et_login_password)
    EditText etPw;
    @BindView(R.id.et_login_again_password)
    EditText etRpw;
    @BindView(R.id.btn_send_code)
    TextView sendCode;
    @BindView(R.id.btn_complete)
    Button complete;
    private BackPasswordPresenter backPasswordPresenter;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_password_retrieval;
    }

    @Override
    protected int getTitleId() {
        return R.string.back_password;
    }

    @Override
    protected void initView() {
        etCode.setInputType(InputType.TYPE_CLASS_NUMBER
                | InputType.TYPE_NUMBER_FLAG_DECIMAL);
    }

    @Override
    protected void initData() {
        backPasswordPresenter = new BackPasswordPresenter(this);
    }

    @OnClick({R.id.btn_send_code, R.id.btn_complete})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_send_code:
                backPasswordPresenter.baseVerification();
                backPasswordPresenter.sendCode();
                timer.start();
                break;
            case R.id.btn_complete:
                backPasswordPresenter.resetPwd();
                break;
        }
    }


    public void onEventMainThread(OrderEvent result) {
        finish();
    }

    @Override
    public String getPhone() {
        return etPhone.getText().toString().trim();
    }

    @Override
    public String getCode() {
        return etCode.getText().toString().trim();
    }

    @Override
    public String getPw() {
        return etPw.getText().toString().trim();
    }

    @Override
    public String getRpw() {
        return etRpw.getText().toString().trim();
    }

    @Override
    public void onSuccess(String msg, boolean b) {
        ToastUtil.show(mContext, msg);
        if (b) {
            EventBus.getDefault().post(new OrderEvent());
        } else {

        }
    }

    @Override
    public void onShowError(String msg) {
        ToastUtil.show(mContext, msg);
    }

    private CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            sendCode.setText((millisUntilFinished / 1000) + "秒后可重发");
            sendCode.setBackgroundResource(R.color.title_DDD);
            sendCode.setClickable(false);
        }

        @Override
        public void onFinish() {
            sendCode.setEnabled(true);
            sendCode.setBackgroundResource(R.color.title_orange);
            sendCode.setClickable(true);
            sendCode.setText("获取验证码");

        }
    };
}
